package com.ruoyi.see.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.see.domain.ArticleSee;
import com.ruoyi.see.service.IArticleSeeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章访问Controller
 *
 * @author ruoyi
 * @date 2024-12-10
 */
@RestController
@RequestMapping("/see/see")
public class ArticleSeeController extends BaseController {
    @Autowired
    private IArticleSeeService articleSeeService;

    /**
     * 查询文章访问列表
     */
    @PreAuthorize("@ss.hasPermi('see:see:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleSee articleSee) {
        startPage();
        List<ArticleSee> list = articleSeeService.selectArticleSeeList(articleSee);
        return getDataTable(list);
    }

    /**
     * 查询文章访问量
     */
    @PreAuthorize("@ss.hasPermi('see:see:list')")
    @GetMapping("/count")
    public int count(ArticleSee articleSee) {
        return articleSeeService.countArticleSeeByUid(articleSee);
    }

    /**
     * 导出文章访问列表
     */
    @PreAuthorize("@ss.hasPermi('see:see:export')")
    @Log(title = "文章访问", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArticleSee articleSee) {
        List<ArticleSee> list = articleSeeService.selectArticleSeeList(articleSee);
        ExcelUtil<ArticleSee> util = new ExcelUtil<ArticleSee>(ArticleSee.class);
        util.exportExcel(response, list, "文章访问数据");
    }

    /**
     * 获取文章访问详细信息
     */
    @PreAuthorize("@ss.hasPermi('see:see:query')")
    @GetMapping(value = "/{uid}")
    public AjaxResult getInfo(@PathVariable("uid") Long uid) {
        return success(articleSeeService.selectArticleSeeByUid(uid));
    }

    /**
     * 新增文章访问
     */
    @PreAuthorize("@ss.hasPermi('see:see:add')")
    @Log(title = "文章访问", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleSee articleSee) {
        return toAjax(articleSeeService.insertArticleSee(articleSee));
    }

    /**
     * 修改文章访问
     */
    @PreAuthorize("@ss.hasPermi('see:see:edit')")
    @Log(title = "文章访问", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticleSee articleSee) {
        return toAjax(articleSeeService.updateArticleSee(articleSee));
    }

    /**
     * 删除文章访问
     */
    @PreAuthorize("@ss.hasPermi('see:see:remove')")
    @Log(title = "文章访问", businessType = BusinessType.DELETE)
    @DeleteMapping("/{uids}")
    public AjaxResult remove(@PathVariable Long[] uids) {
        return toAjax(articleSeeService.deleteArticleSeeByUids(uids));
    }

    /*
    * 查询周访问量
    * */
    @GetMapping("/getWeekSee")
    public AjaxResult getWeekSee(ArticleSee articleSee) {
        return success(articleSeeService.selectArticleSeeCountsByWeekForArticleId(articleSee));
    }

    @GetMapping("/getHourSee")
    public AjaxResult getHourSee(ArticleSee articleSee) {
        return success(articleSeeService.selectArticleSeeCountsByHourForArticleId(articleSee));
    }

    @GetMapping("/getMonthSee")
    public AjaxResult getMonthSee(ArticleSee articleSee) {
        return success(articleSeeService.selectArticleSeeCountsByMonthForArticleId(articleSee));
    }

    @GetMapping("/getAllSee")
    public AjaxResult getAllSee(ArticleSee articleSee) {
        return success(articleSeeService.countArticleSeesByArticleIdAndDate(articleSee));
    }
}
